package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class NotificationDAO extends CrudDAO<Notification>{

    @Override
    public Notification create(Notification notificacion) {
        String sqlQuerry = "INSERT INTO Notification (mail_notified, note, state, notification_date) VALUES ('"
        		+notificacion.getMail_notified()+"','"+notificacion.getNote()+"','"+notificacion.getState()+"','"
        		+notificacion.getDate()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(Notification notificacion) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM Notification WHERE idNotification='"+notificacion.getId()+"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public Notification read(Notification notificacion) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM Notification WHERE idNotification=" + notificacion.getId()+ "");
        if (rs != null) {
            try {
                while (rs.next()) {
                    notificacion.setId(rs.getInt("idNotification"));
                    notificacion.setMail_notified(rs.getString("mail_notified"));
                    notificacion.setNote(rs.getString("note"));
                    notificacion.setState(rs.getString("state"));
					notificacion.setDate(rs.getString("notification_date"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return notificacion;
    }

    @Override
    public void update(Notification notificacion) {
    	 String sqlQuerry="UPDATE Notification SET mail_notified='"+notificacion.getMail_notified()
		         +"', notee='" + notificacion.getNote()
		         +"', state='" +notificacion.getState()
		         +"', notification_date='"+notificacion.getDate()
		         + "' WHERE idNotification='" + notificacion.getId()+ "'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
